/**
 * @file Diag_OtaUDSConfig.c
 * @author dowson
 * @brief 
 * @version 0.1
 * @date 2023-08-17
 * 
 * @copyright Copyright (c) 2023
 * 
 */
#include "stdint.h"
#include "stdlib.h"
#include "string.h"
#include "string.h"

#include "Diag_UDSClient.h"
#include "App_OtaUDSUpgradeMain.h"
#include "system_init.h"
#include "OtaData.h"

#include "Diag_OtaUDSConfig.h"

#ifdef PARTFROM_AC7

extern t_OtaStateManage OtaStateManage;
extern Diag_UDSClientType UDSClient;
extern uint8_t UDSKeyValue_0x27[4];
static const uint8_t ANPEI_RoutineCtrlData[9] = {0x44,0x08,0x00,0x80,0x00,0x00,0x01,0x1D,0x30};
static const uint8_t ANPEI_RoutineCtrlEndData[4] = {0x96,0x73,0xbf,0x44};
static const uint8_t GANFENG_RoutineCtrlEndData[4] = {0x75,0x07,0x86,0xa5};
static const uint8_t YINGJIELI_RoutineCtrlData[9] = {0x44,0x00,0xc1,0x00,0x00,0x00,0x01,0x31,0x30};
static const uint8_t YINGJIELI_RoutineCtrlMiddleData[4] = {0xf7,0x29,0x19,0x52};
static const uint8_t YINGJIELI_RoutineCtrlSecondMiddleData[9] = {0x44,0x00,0xc2,0x60,0x00,0x00,0x00,0x11,0x80};
static const uint8_t YINGJIELI_RoutineCtrlEndData[4] = {0x52,0x99,0x0b,0x3e};


Diag_ErrType Diag_Request_0x27_CallBack(Diag_UDSClientType *client){

    uint32_t UDSSeed = 0;
    uint32_t UDSKey = 0;
    uint8_t loopTimes = 32;
    memset(UDSKeyValue_0x27,0x00,sizeof(UDSKeyValue_0x27));
    //fprintf(USART2_STREAM,"-timeOutState:%d\r\n",client->RecvDataType.timeOutState);     
    if(!client->RecvDataType.timeOutState){
        //fprintf(USART2_STREAM,"-0x27-recvSize:%d\r\n",client->recvSize);    
        UDSSeed = (UDSSeed = ((UDSSeed = client->recvBuf[3])<<8 | client->recvBuf[4]) << 8 | client->recvBuf[5]) << 8 | client->recvBuf[6];
        if (AC7_2104010 == OtaStateManage.deviceId) loopTimes = 35;
        else loopTimes = 32;
        //fprintf(USART2_STREAM,"get seed:%x,ecutype:%d,loopTime:%d",UDSSeed,client->ecuType,loopTimes);
        UDSKey = Diag_KeyComputation(UDSSeed,client->ecuType,loopTimes);
        //fprintf(USART2_STREAM,"get key:%x",UDSKey);
        UDSKeyValue_0x27[0] = (UDSKey >> 24) & 0xff;
        UDSKeyValue_0x27[1] = (UDSKey >> 16) & 0xff;
        UDSKeyValue_0x27[2] = (UDSKey >> 8) & 0xff;
        UDSKeyValue_0x27[3] = (UDSKey >> 0) & 0xff;   

        client->RecvDataType.responseState = RequestResponseState(client->sid,client->recvBuf,client->recvSize);
    }else{
        client->RecvDataType.responseState = Diag_RESPONSE_ERR;
            return Diag_ERR_TIMEOUT;
    }
    return Diag_OK;
}

//------------------------------------------ANPEI--------------------------------------------------

Diag_SquenceUDSType ANPEI_ShakeHandSquence[12] = {
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_ExtendedDiagnostic}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_Request_0x27_CallBack,3,{0x01,(uint32_t)NULL,0}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,3,{0x02,(uint32_t)UDSKeyValue_0x27,4}},
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_FUNC_ID,Diag_RequestCommCallBack,1,{Diag_DefaultSession}},
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_FUNC_ID,Diag_RequestCommCallBack,1,{Diag_ExtendedDiagnostic}},
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,4,{Diag_StartRoutine,0x0203,(uint32_t)NULL,0}},
   {Diag_0X85_DTCControlFunc,Diag_REQ_IDEL,Diag_FUNC_ID,Diag_RequestCommCallBack,1,{Diag_DtcOff}},
   {Diag_0X28_SendCommCtrlFunc,Diag_REQ_IDEL,Diag_FUNC_ID,Diag_RequestCommCallBack,2,{Diag_DisableRxAndTx,Diag_NormalCommunicationMessages}},
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_ProgrammingSession}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_Request_0x27_CallBack,3,{0x011,(uint32_t)NULL,0}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,3,{0x012,(uint32_t)UDSKeyValue_0x27,4}},
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,4,{Diag_StartRoutine,0xff00,(uint32_t)ANPEI_RoutineCtrlData,COUNT_OF_TABLE(ANPEI_RoutineCtrlData)}},
};

Diag_SquenceUDSListType ANPEI_ShakeHandSquenceList = {
    .SquenceNodeListHead = ANPEI_ShakeHandSquence,
    .listSize = 12,
    .ret = Diag_OK,
    .currentItem = 0,
    .client = &UDSClient,
    .ecuType = UDS_MCU,
    .phyId = 0x102,
    .responseId = 0x602,
};


Diag_SquenceUDSType ANPEI_DeShakeHandSquence[2] = {
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_StartRoutine,0x0202,(uint32_t)ANPEI_RoutineCtrlEndData,COUNT_OF_TABLE(ANPEI_RoutineCtrlEndData)}},
   {Diag_0X11_SendEcuResetFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{0x01}},
};

Diag_SquenceUDSListType ANPEI_DeShakeHandSquenceList = {
    .SquenceNodeListHead = ANPEI_DeShakeHandSquence,
    .listSize = 2,
    .ret = Diag_OK,
    .currentItem = 0,
    .client = &UDSClient,
    .ecuType = UDS_MCU,
    .phyId = 0x102,
    .responseId = 0x602,
};

//-------------------------------------------------------------------------------------------------

//--------------------------------------YINGJILIE----------------------------------------------------

Diag_SquenceUDSType YINGJIELI_ShakeHandSquence[13] = {
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,4,{Diag_StartRoutine,0x0202,(uint32_t)NULL,0}},
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_ExtendedDiagnostic}},
   {Diag_0X85_DTCControlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_DtcOff}},
   {Diag_0X28_SendCommCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,2,{Diag_DisableRxAndTx,Diag_NormalCommunicationMessages}},
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_ProgrammingSession}},
   {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
   {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
   {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
   {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
   {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_Request_0x27_CallBack,3,{0x09,(uint32_t)NULL,0}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,3,{0x0a,(uint32_t)UDSKeyValue_0x27,4}},
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,4,{Diag_StartRoutine,0xff00,(uint32_t)YINGJIELI_RoutineCtrlData,COUNT_OF_TABLE(YINGJIELI_RoutineCtrlData)}},
};

Diag_SquenceUDSListType YINGJIELI_ShakeHandSquenceList = {
    .SquenceNodeListHead = YINGJIELI_ShakeHandSquence,
    .listSize = 13,
    .ret = Diag_OK,
    .currentItem = 0,
    .client = &UDSClient,
    .ecuType = UDS_MCU,
    .phyId = 0x102,
    .responseId = 0x602,
};


Diag_SquenceUDSType YINGJIELI_ContinueShakeHandSquence[7] = {
    {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
    {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
    {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
    {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
    {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
    {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,3,{Diag_StartRoutine,0xf001,(uint32_t)YINGJIELI_RoutineCtrlMiddleData,COUNT_OF_TABLE(YINGJIELI_RoutineCtrlMiddleData)}},
    {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,3,{Diag_StartRoutine,0xff00,(uint32_t)YINGJIELI_RoutineCtrlSecondMiddleData,COUNT_OF_TABLE(YINGJIELI_RoutineCtrlSecondMiddleData)}},
};

Diag_SquenceUDSListType YINGJIELI_ContinueShakeHandSquenceList = {
    .SquenceNodeListHead = YINGJIELI_ContinueShakeHandSquence,
    .listSize = 7,
    .ret = Diag_OK,
    .currentItem = 0,
    .client = &UDSClient,
    .ecuType = UDS_MCU,
    .phyId = 0x102,
    .responseId = 0x602,
};

Diag_SquenceUDSType YINGJIELI_DeShakeHandSquence[4] = {
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,3,{Diag_StartRoutine,0xf001,(uint32_t)YINGJIELI_RoutineCtrlEndData,COUNT_OF_TABLE(YINGJIELI_RoutineCtrlEndData)}},
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,3,{Diag_StartRoutine,0xff01,(uint32_t)NULL,0}},
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_ExtendedDiagnostic}},
   {Diag_0X11_SendEcuResetFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{0x01}},
};

Diag_SquenceUDSListType YINGJIELI_DeShakeHandSquenceList = {
    .SquenceNodeListHead = YINGJIELI_DeShakeHandSquence,
    .listSize = 4,
    .ret = Diag_OK,
    .currentItem = 0,
    .client = &UDSClient,
    .ecuType = UDS_MCU,
    .phyId = 0x102,
    .responseId = 0x602,
};





//-------------------------------------------------------------------------------------------------


//-----------------------------------------GANFENG---------------------------------------------------
Diag_SquenceUDSType GANFENG_ShakeHandSquence[17] = {
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_ExtendedDiagnostic}},
   {Diag_0X85_DTCControlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_DtcOff}},
   {Diag_0X28_SendCommCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,2,{Diag_DisableRxAndTx,Diag_NormalCommunicationMessages}},
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_ProgrammingSession}},
   {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
   {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
   {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
   {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
   {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
   {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
   {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
   {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
   {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
   {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
   {Diag_0X3E_SendTesterPresentFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,0,{0}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_Request_0x27_CallBack,3,{0x01,(uint32_t)NULL,0}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,3,{0x02,(uint32_t)UDSKeyValue_0x27,2}},
};

Diag_SquenceUDSListType GANFENG_ShakeHandSquenceList = {
    .SquenceNodeListHead = GANFENG_ShakeHandSquence,
    .listSize = 17,
    .ret = Diag_OK,
    .currentItem = 0,
    .client = &UDSClient,
    .ecuType = UDS_BMS,
    .phyId = 0x101,
    .responseId = 0x601,
};


Diag_SquenceUDSType GANFENG_DeShakeHandSquence[3] = {
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_StartRoutine,0xf001,(uint32_t)GANFENG_RoutineCtrlEndData,COUNT_OF_TABLE(GANFENG_RoutineCtrlEndData)}},
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_StartRoutine,0xff01,(uint32_t)NULL,0}},
   //{Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_ExtendedDiagnostic}},
   {Diag_0X11_SendEcuResetFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{0x01}},
};

Diag_SquenceUDSListType GANFENG_DeShakeHandSquenceList = {
    .SquenceNodeListHead = GANFENG_DeShakeHandSquence,
    .listSize = 3,
    .ret = Diag_OK,
    .currentItem = 0,
    .client = &UDSClient,
    .ecuType = UDS_BMS,
    .phyId = 0x101,
    .responseId = 0x601,
};


//-------------------------------------------------------------------------------------------------

Diag_SquenceUDSListType *Diag_SquenceUDSListCfg[7] = {
    [ANPEI_START] = &ANPEI_ShakeHandSquenceList,
    [ANPEI_END] = &ANPEI_DeShakeHandSquenceList,
    [GANFENG_START] = &GANFENG_ShakeHandSquenceList,
    [GANFENG_END] = &GANFENG_DeShakeHandSquenceList,
    [YINGJIELI_START] = &YINGJIELI_ShakeHandSquenceList,
    [YINGJIELI_CONTINUE] = &YINGJIELI_ContinueShakeHandSquenceList,
    [YINGJIELI_END] = &YINGJIELI_DeShakeHandSquenceList
};

#endif
